package top.lyc829.zzl_android.ui.profile.user_dtc.travels;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import java.io.IOException;
import java.util.List;

import okhttp3.Response;
import top.lyc829.zzl_android.logic.bean.ResultBean;
import top.lyc829.zzl_android.logic.bean.TravelsSimpleBean;
import top.lyc829.zzl_android.logic.network.Network;
import top.lyc829.zzl_android.logic.util.HttpUtil;
import top.lyc829.zzl_android.logic.util.Utils;
import top.lyc829.zzl_android.logic.util.gson.JsonUtilNew;

public class UserTravelsViewModel extends ViewModel {

    private MutableLiveData<List<TravelsSimpleBean>> userTravelsLiveData;

    public MutableLiveData<List<TravelsSimpleBean>> getUserTravelsLiveData() {
        if(userTravelsLiveData==null)
        {
            userTravelsLiveData=new MutableLiveData<>();
        }
        return userTravelsLiveData;
    }

    //端口问题....
    public void getUserTravelsList(int user_id){
        Network.getUserTravelsList( user_id, new HttpUtil.MyCallback() {
            @Override
            public void success(Response res) throws IOException {
                String resultStr=res.body().string();
                ResultBean<List<TravelsSimpleBean>> resultBean= JsonUtilNew.fromJsonArray(resultStr,TravelsSimpleBean.class);
                if (resultBean.getCode().equals(200))
                {
                    getUserTravelsLiveData().postValue(resultBean.getData());
                }
                else{

                    failed(new IOException(resultBean.getMsg()));
                }
            }

            @Override
            public void failed(IOException e) {
                getUserTravelsLiveData().postValue(null);
                Utils.Sout(e.getMessage());
            }
        });
    }
}
